#include "DAOArticle.h"

DAOArticle::DAOArticle(const DBInfo& dbInfo)
{
  //m_logMsg = new char[SIZE_LOG_MSG];
  int result = 0;
  char *query = NULL;
  m_connection = mysql_init((MYSQL*)0);
  if ( m_connection != NULL && mysql_real_connect(m_connection,dbInfo.dbip.c_str(), dbInfo.dbuser.c_str(), dbInfo.dbpasswd.c_str(), dbInfo.dbname.c_str(), dbInfo.port, NULL, 0))
  {
    if (!mysql_select_db(m_connection,dbInfo.dbname.c_str())) 
    {
      m_connection ->reconnect = 1; 
      query = "set names \'GBK\'";
      result = mysql_real_query(m_connection, query, strlen(query));
      if(result)
      {
        printf("[10001]Error making query: %s!", mysql_error(m_connection));
      }
    }
  }
  else
  {
    printf("[10002]Unable to connect the database,check your configuration!");
  }
  m_articleIndex=0;
}

DAOArticle::~DAOArticle(void)
{
  if(NULL != m_connection)
  {
    mysql_close(m_connection);
    m_connection = NULL;
  }
}

bool DAOArticle::addArticle(const Article& article)
{
  int result = 0;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql,SQL_ARTICLE_INSERT,
    article.articleid.c_str(), article.siteid.c_str(), article.postdate.c_str(), article.lastupdate.c_str(), article.articlename.c_str(), article.keywords.c_str(),
    article.initial, article.authorid.c_str(), article.author.c_str(), article.posterid.c_str(), article.poster.c_str(), article.agentid.c_str(),
    article.agent.c_str(), article.sortid.c_str(), article.ctypeid.c_str(), article.intro.c_str(), article.notice.c_str(), article.setting.c_str(),
    article.lastvolumeid.c_str(), article.lastvolume.c_str(), article.lastchapterid.c_str(), article.lastchapter.c_str(), article.chapters.c_str(), article.size.c_str(),
    article.lastvisit.c_str(), article.dayvisit.c_str(), article.weekvisit.c_str(), article.monthvisit.c_str(), article.allvisit.c_str(), article.lastvote.c_str(),
    article.dayvote.c_str(), article.weekvote.c_str(), article.monthvote.c_str(), article.allvote.c_str(), article.vipvotetime.c_str(), article.vipvotenow.c_str(),
    article.vipvotepreview.c_str(), article.goodnum.c_str(), article.badnum.c_str(), article.toptime.c_str(), article.saleprice.c_str(), article.salenum.c_str(),
    article.totalcost.c_str(), article.articletype, article.permission, article.firstflag, article.fullflag, article.imgflag,
    article.power, article.display,article.downloadlink.c_str()
    );
  result = mysql_real_query(m_connection, sql, strlen(sql));
  return !result;
}

bool DAOArticle::getArticle(const string articleName, Article& article, bool& isExist)
{
  int result;
  isExist = false;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_ARTICLE_SELECT_BYNAME, articleName.c_str());
  result = mysql_real_query(m_connection, sql, strlen(sql));
  if(result)
  {
    printf("[10006]Query faild!%s!", sql);
    return false;
  }
  m_res = mysql_store_result(m_connection);
  while((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    setFieldValue(&article);
    isExist = true;
  }
  mysql_free_result(m_res);
  m_res = NULL;
  return true;
}

int DAOArticle::getArticleListCount()
{
  int result;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_ARTICLE_SELECT_LIST_COUNT);
  result = mysql_real_query(m_connection, sql, strlen(sql));
  if(result)
  {
    printf("[10008]Query faild!%s", sql);
    //sprintf(m_logMsg, "[%s]Query faild!%s", sql);
    //Log::error(m_logMsg);
    return -1;
  }
  m_res = mysql_store_result(m_connection);
  while((m_row = mysql_fetch_row(m_res)) == 0)  
  {
    return atoi(m_row[0]);
  }
  mysql_free_result(m_res); 
  m_res = NULL;
  return -1;
}

bool DAOArticle::getArticleList(const string& lastupdate,Article* articleList,int& recCount)
{
  int result;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_ARTICLE_SELECT_LIST, lastupdate.c_str());
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_ARTICLEDAO_109, sql);
  //Log::debug(m_logMsg);
  //#endif
  result = mysql_real_query(m_connection, sql, strlen(sql));
  if(result)
  {
    printf("[10110]Select article list failed!");
    //sprintf(m_logMsg, "[%s]Select article list failed!", MSG_ARTICLEDAO_110);
    //Log::error(m_logMsg);
    return false;
  }
  m_res = mysql_store_result(m_connection);
  while((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    setFieldValue(articleList);
    recCount++;
    articleList++;
  }
  mysql_free_result(m_res); 
  m_res = NULL;
  return true;
}

bool DAOArticle::updateArticle(const Article& article)
{
  int result = 0;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql,SQL_ARTICLE_UPDATE,
    article.lastvolumeid.c_str(), article.lastvolume.c_str(), article.lastchapterid.c_str(), article.lastchapter.c_str(),
    article.chapters.c_str(), article.size.c_str(),
    article.articleid.c_str());
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_ARTICLEDAO_111, sql);
  //Log::debug(m_logMsg);
  //#endif
  result = mysql_real_query(m_connection, sql, strlen(sql));
  return !result;
}

string DAOArticle::getArticleMaxID()
{
  string result;
  int rt;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_ARTICLE_MAXID);
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_ARTICLEDAO_112, sql);
  //Log::debug(m_logMsg);
  //#endif
  rt = mysql_real_query(m_connection, sql, strlen(sql));
  if(rt)
  {
    printf("[10113]%s!", sql);
    //#ifdef _DEBUG
    //sprintf(m_logMsg, "[%s]%s!", MSG_ARTICLEDAO_113, sql);
    //Log::debug(m_logMsg);
    //#endif
    return "";
  }
  m_res = mysql_store_result(m_connection);
  char tmp[12]={0};
  while((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    if(m_row[0]!=NULL && strlen(m_row[0])>0)
    {
      sprintf(tmp,"%d",atoi(m_row[0])+1);
      result.assign(tmp);
    }
  }
  mysql_free_result(m_res); 
  m_res = NULL;
  if(result.length() == 0)
  {
    result = "1";
  }
  return result;
}